<?php
/**
 * Custom Vendor model.
 * Author : MSE
 * Date   : 01 June 2010
 * Project: shopping.mu
 */
class ModelCatalogVendor extends Model {
	
	/**
	 * Adds a new Vendor.
	 */
	public function addVendor($data) {
	
		// Create the vendor
      	$this->db->query("INSERT INTO " . DB_PREFIX . "vendor SET name = '" . $this->db->escape($data['name']) . "', sort_order = '" . (int)$data['sort_order'] . "'");
		
		// Get vendor_id
		$vendor_id = $this->db->getLastId();

		// Address
		if (isset($data['address'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET address = '" . $this->db->escape($data['address']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Telephone
		if (isset($data['telephone'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET telephone = '" . $this->db->escape($data['telephone']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Email
		if (isset($data['email'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET email = '" . $this->db->escape($data['email']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Website
		if (isset($data['website'])) {
		
			$website = $data['website'];
			
			// Append http:// to the beginning if its not done already
			if (strpos($data['website'], 'http://') === false) {
				$website = 'http://' . $data['website'];
			}
			
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET website = '" . $this->db->escape($website) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Image
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET image = '" . $this->db->escape($data['image']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Details
		if (isset($data['details'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET details = '" . $this->db->escape($data['details']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		/*
		if (isset($data['manufacturer_store'])) {
			foreach ($data['manufacturer_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "manufacturer_to_store SET manufacturer_id = '" . (int)$manufacturer_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'manufacturer_id=" . (int)$manufacturer_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		*/
		
		$this->cache->delete('vendor');
	}
	
	/**
	 * Edit vendor
	 */
	public function editVendor($vendor_id, $data) {
	
		// Name
      	$this->db->query("UPDATE " . DB_PREFIX . "vendor SET name = '" . $this->db->escape($data['name']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE vendor_id = '" . (int)$vendor_id . "'");

		// Address
		if (isset($data['address'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET address = '" . $this->db->escape($data['address']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Telephone
		if (isset($data['telephone'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET telephone = '" . $this->db->escape($data['telephone']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Email
		if (isset($data['email'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET email = '" . $this->db->escape($data['email']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Website
		if (isset($data['website'])) {
		
			$website = $data['website'];
			
			// Append http:// to the beginning if its not done already
			if (strpos($data['website'], 'http://') === false) {
				$website = 'http://' . $data['website'];
			}
			
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET website = '" . $this->db->escape($website) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Image
		if (isset($data['image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET image = '" . $this->db->escape($data['image']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		// Details
		if (isset($data['details'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "vendor SET details = '" . $this->db->escape($data['details']) . "' WHERE vendor_id = '" . (int)$vendor_id . "'");
		}
		
		/*
		$this->db->query("DELETE FROM " . DB_PREFIX . "manufacturer_to_store WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");

		if (isset($data['manufacturer_store'])) {
			foreach ($data['manufacturer_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "manufacturer_to_store SET manufacturer_id = '" . (int)$manufacturer_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'manufacturer_id=" . (int)$manufacturer_id. "'");
		
		if ($data['keyword']) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'manufacturer_id=" . (int)$manufacturer_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
		}
		*/
		$this->cache->delete('vendor');
	}
	
	/**
	 * Delete vendor.
	 */
	public function deleteVendor($vendor_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "vendor WHERE vendor_id = '" . (int)$vendor_id . "'");
		
		/*
		$this->db->query("DELETE FROM " . DB_PREFIX . "manufacturer_to_store WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'manufacturer_id=" . (int)$manufacturer_id . "'");
			*/
		$this->cache->delete('vendor');
	}	
	
	/**
	 * Retrieve a vendor.
	 */
	public function getVendor($vendor_id) {
	/*
		$query = $this->db->query("SELECT DISTINCT *, (SELECT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'vendor_id=" . (int)$vendor_id . "') AS keyword FROM " . DB_PREFIX . "vendor WHERE vendor_id = '" . (int)$vendor_id . "'");
		*/
		
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "vendor WHERE vendor_id = '" . (int)$vendor_id . "'");
		
		return $query->row;
	}
	
	/**
	 * Get multiple vendors
	 */
	public function getVendors($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "vendor";
			
			$sort_data = array(
				'name',
				'sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$vendor_data = $this->cache->get('vendor');
		
			if (!$vendor_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "vendor ORDER BY name");
	
				$vendor_data = $query->rows;
			
				$this->cache->set('vendor', $vendor_data);
			}
		 
			return $vendor_data;
		}
	}

	/*
	public function getManufacturerStores($manufacturer_id) {
		$manufacturer_store_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "manufacturer_to_store WHERE manufacturer_id = '" . (int)$manufacturer_id . "'");

		foreach ($query->rows as $result) {
			$manufacturer_store_data[] = $result['store_id'];
		}
		
		return $manufacturer_store_data;
	}
	
	public function getTotalManufacturersByImageId($image_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "manufacturer WHERE image_id = '" . (int)$image_id . "'");

		return $query->row['total'];
	}
	*/
	
	/**
	 * Count total vendors found.
	 */
	public function getTotalVendors() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "vendor");
		
		return $query->row['total'];
	}	
}
?>